<?php
namespace Api\Controller;
use Think\Controller;
class PortController extends Controller {

    /**
     * 联系我们
     *
     */
    public  function get_contact(){
        $result=M('contact')->where(array('id'=>1))->find();
        $this->ajaxReturn($result,'json');
    }

    /**
     * 品牌合作
     *
     */
    public function get_cooperation(){
        $data=M('cooperation')->where(array('id'=>1))->find();
        $data['icon']=explode(',',$data['icon']);
        $data['content']=explode(',',$data['content']);
        $data['name']=explode(',',$data['name']);
        $data['data']['data']=hebingarr($data['name'],$data['content'],$data['icon'],'name','content','icon');
        $data['data']['images']=$data['images'];
        $this->ajaxReturn($data['data'],'json');
    }

    /**
     * 获取服务页面数据
     */
    public function get_services(){
//        查询服务分类
        $data['service']=M('service_type')->select();
        foreach($data['service'] as $k => $v){
            $data['list'][]=M('service')->where(array('service_type'=>$v['id']))->order('add_time desc,id asc')->select();
        }
        $this->ajaxReturn($data,'json');
    }

    /**
     * 获取服务详情
     *
     */
    public function get_services_details(){
        $id=I('id');
        $data=M('service')->where(array('id'=>$id))->find();
        $this->ajaxReturn($data,'json');
    }

    /**
     * 获取品牌简介数据
     *
     */
    public function get_introduce(){
        $data = M('about')->where('id=1')->find();
        $data1=M('contact')->where('id=1')->find();
        $data['longitude']=$data1['longitude'];
        $data['latitude']=$data1['latitude'];
        $this->ajaxReturn($data,'json');
    }
    /**
     * 获取新闻
     *
     */
    public function get_news(){
        $data['newtype']=M('news_type')->select();
        foreach ($data['newtype'] as $key=>$val ){
            $data['list'][]=M('news')->where(array('news_type'=>$key))->select();
        }
        $arrcount=count($data['newtype']);
        for($i=0;$i<$arrcount;$i++){
             $count=count($data['list'][$i]);
                for($a=0;$a<$count;$a++){
                    $data['list'][$i][$a]['add_time']=date("Y-m-d H:i", $data['list'][$i][$a]['add_time']);
                }
        }
        $this->ajaxReturn($data,'json');
    }

    /**
     * 获取新闻详情
     *
     */
    public function get_news_details(){
        $id=I('id');
        $data=M('news')->where(array('id'=>$id))->find();
        $data['add_time']=date("Y-m-d H:i",$data['add_time']);
        $this->ajaxReturn($data,'json');
    }

    /**
     * 首页获取数据
     *
     */
    public function get_homepage(){
        $news=M('news');
        $data = M('homepage')->where(array('id'=>1))->find();
        $data['images']=explode(',',$data['images']);
        $data['service']=M('service')->where(array('top'=>1))->order('id')->select();
        $data['gongsidongtai']=$news->where('news_type=0 And top=1')->order('id')->select();
        foreach ($data['gongsidongtai'] as $key=>$val){
            $data['gongsidongtai'][$key]['add_time']=date("Y-m-d H:i", $data['gongsidongtai'][$key]['add_time']);
        }
        $data['hanyexinwen']=$news->where('news_type=1 And top=1')->order('id')->select();
        foreach ($data['hanyexinwen'] as $key=>$val){
            $data['hanyexinwen'][$key]['add_time']=date("Y-m-d H:i", $data['hanyexinwen'][$key]['add_time']);
        }
        $datas['icon']=explode(',',$data['icon']);
        $datas['icon_name']=explode(',',$data['icon_name']);
        $datas['icon_url']=explode(',',$data['icon_url']);
        $data['tubiao']=hebingarr($datas['icon'],$datas['icon_name'],$datas['icon_url']);
        $data['about']=M('about')->where(array('id'=>1))->find();
        $this->ajaxReturn($data,'json');
    }

    /**
     * 留言板
     **/
    public  function add_message()
    {
        if(IS_POST){
            $m = M('message');
            if (!$m->autoCheckToken($_POST)) {
                $this->error('请勿重复提交', 0, 0);
            } else {
                    $data = I();
                    $data['add_time'] = time();
                if ($m->add($data)) {
                    $this->ajaxReturn(array('msg' => '添加失败'));
                } else {
                    $this->ajaxReturn(array('msg' => '添加成功'));
                }
            }
        }else{
            $this->ajaxReturn(array('msg' => '提交失败'));
        }


    }
}
